home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ian & Stuart's Australian Mac 1
/
Ian and Stuart's One (Australia).iso
/
Australasian Legends
/
Commercial
/
Rainbow Hill
/
MacDOS™ 2.0.0
/
User's Guide
/
07 Global Variables
< prev
next >
Wrap
Text File
|
1994-09-20
|
12KB
|
290 lines
7 Global Variables
Global variables are just a way of saving character strings
for later use. They are referred to as "global" because
they are accessible from within all batch programs as well
as interactively.
MacDOS automatically generates and uses some variables
called "System Variables".
Creating and Setting Variables
You can set global variables interactively or in batch
programs with the command SET. When you SET a variable for
the first time, MacDOS automatically creates it. The format
of SET is very simple:
set variable name=whatever string you like
Any string can be used to name such user-defined variables,
but be aware that:
• If you include a percent sign, this will confuse
MacDOS when you try to use the variable in a command,
because percent signs are used to access the value of
variables (see below).
• If you include double quotes or other special
characters like colons, slashes, and backslashes you
might run into problems later when you use the variable
in batch programs. At the very least, you will find it
confusing when reading your program.
• When recording a variable name, MacDOS ignores
leading and trailing spaces and tabs. That is, you will
not be able to create a variable name which begins or
ends with blanks.
• MacDOS also simplifies all sequences of spaces and
tabs found within variable names, so that words forming
a variable name are always separated by single spaces.
The string which follows the equal sign is stored as it is,
including leading and trailing blanks.
When you SET an existing variable, MacDOS simply replaces
the current value with the new one.
Displaying and Removing Variables
To display a list of the existing variables and their
content, just type the command SET without parameters.
MacDOS then displays all the variables in the order in
which they were created.
You can also display the string stored in a single variable
by typing:
ECHO %varName%
When you do not need a user-defined variable anymore, it is
a good practice to remove it from the system. To do so, SET
the variable to an empty string by typing return
immediately after the equal sign. System variables are
special, in that they cannot be removed from the system.
When you SET a system variable to the empty string, MacDOS
does not remove it but sets it back to its default value.
Retrieving Variables
You can access the string stored in a variable by inserting
in a command the variable name enclosed between a pair of
percent signs. For example, you can define the variable
HOME by executing SET HOME=%WHERE% and then attach back to
that folder with CHDIR %HOME% . Similarly, if the variable
RET_LBL contains a label name, you can jump to that label
with the command GOTO %RET_LBL% .
If you type two consecutive percent signs, MacDOS
recognises that you are not attempting to de-reference a
variable or a replaceable parameter and handles the pair of
characters as a single percent sign.
Operations on Variables
MacDOS provides a series of commands which let you
manipulate global variables. They are: INCR, DECR, SSTR,
and TOUPPER. If you need to keep the original value, use
SET to save it before executing any of these commands.
INCR
INCR has the purpose of incrementing numeric values, adding
numbers, and extending or concatenating alphanumeric
strings.
incr numVar
incr numVar by 7
incr numVar by -3
are examples in which INCR is used to increment the numeric
value stored in the global variable numVar. In the last
example, the final value is actually less than the initial
one, because the increment is negative.
incr numVar by %anotherNumVar%
shows how INCR can be used to add anotherNumVar to
numVar.
incr var by " append this"
incr -var by "prepend this "
incr var by anAlphaVar
incr alphaVar by 3
incr -alphaVar by 3
are examples of how INCR can be used to attach strings to
each other. In the last two examples, three spaces are
appended and prepended to alphaVar respectively.
DECR
DECR has the purpose of decrementing numeric values,
subtracting numbers, and truncating or subtracting
alphanumeric strings.
decr numVar
decr numVar by 7
decr numVar by -3
are examples in which DECR is used to decrement the numeric
value stored in the global variable numVar. In the last
example, the final value is actually greater than the
initial one, because the decrement is negative.
decr numVar by %anotherNumVar%
shows how DECR can be used to subtract anotherNumVar from
numVar.
decr var by " remove this from the end"
decr -var by "remove this from the beginning"
decr var by anAlphaVar
decr alphaVar by 3
decr -alphaVar by 3
are examples of how DECR can be used to subtract strings
from each other. In the last two examples, the three last
and first characters are removed from alphaVar
respectively.
SSTR
SSTR extracts a substring from a variable (the name SSTR
stands for SubSTRing). In order to identify what should
remain and what should be removed, SSTR uses a string as
delimiter. Its format is:
sstr var delim
where delim is the string which SSTR uses to split the
variable.
By default, SSTR searches for the delimiter from the
beginning of the variable string, and extracts the
substring on the left of the delimiter. Optionally, you can
direct SSTR to search from the end rather than from the
beginning, and to keep the substring on the right of the
delimiter rather than on its left.
TOUPPER
TOUPPER converts the value of a variable to upper case. It
is useful when you want to compare strings and filenames
ignoring upper and lower cases. Note that diacritical marks
and international characters are converted correctly: ü to
Ü, æ to Æ, ø to Ø, etc.
System and Special Variables
Currently, MacDOS implements seven system variables:
PROMPT, DIRCMD, PATH, WHERE, CREATOR, QUOTE, and FONTSIZE.
MacDOS also uses the variables DOSERR, SHOWAE, and TIMEOUT,
which behave like user-defined variables and can therefore
be removed.
PROMPT
stores the format of the string displayed by MacDOS to
prompt you for a new command.
By using a dollar sign as an escape character, PROMPT can
memorise requests to display special information, like the
current date and time, the specification of the current
folder, the current volume ID, and the MacDOS version.
Instead of setting the variable PROMPT with the command
SET, you should use the command PROMPT. This would ensure
that you set the prompt string to a valid value.
By default, the prompt string consists of the current
volume ID followed by a "greater than" sign (PROMPT=$n$g).
Please refer to the command reference section on PROMPT for
a list of dollar-options.
DIRCMD
stores the default options for the commands DIR and TREE.
By default, DIR and TREE list all visible files and folders
in extended format, non recursively, non sorted, and not
case sensitive. Please refer to the command reference
section on DIR for a list of valid options.
PATH
stores the list of folders that MacDOS searches when
looking for batch programs, applications, AppleScripts,
MacDOS extensions, and abbreviation files.
The folder specifications are separated by semicolons.
When you direct MacDOS to execute a program or look for an
abbreviation file, MacDOS searches the current folder for a
file of the appropriate type with the requested name. It
then searches the folders specified in PATH, scanning the
list from left to right.
MacDOS initialises the system variable PATH to the empty
string. Therefore, by default MacDOS only searches the
current folder.
To set the variable PATH you can also use the command with
the same name. To add a folder specification to the
existing list, you can execute any of the following
commands:
set path=%path%;newFolderSpec
path %path%;newFolderSpec
incr path by ;newFolderSpec
WHERE
always contains the specification of the current folder.
MacDOS updates WHERE every time you change volume or
directory.
This variable lets you "go back" to the initial folder
after using CHDIR to attach to other folders and volumes.
To do this, you must save WHERE by copying it to another
variable before attaching to the new folder. For example:
set back=%where%
chdir anotherFolder
...
chdir %back%
CREATOR
stores the file creator that MacDOS uses when creating new
files. By default, the creator is 'ttxt' (ie. TeachText).
You can set it to the creator of your preferred editor or
word processor. You can also set it to 'mDOS' if you want
to create batch programs. The alternative would be to
create the files and then use REN to change their creators.
Note that MacDOS only uses the first four characters of
this variable. Therefore, you can use the rest of the line
for comments.
QUOTE
stores the character used by MacDOS to delimit file and
folder names which contain spaces and other special
characters. The default is the double quote character.
By changing QUOTE, you can access file names which contain
double quote characters:
set quote=#
rename #with "quotes"# #without quotes#
del bla"bla
set quote=
FONTSIZE
stores the point size of the characters used in the console
window.
The default is 9, but the values 12, 18, 24, and 36 are
also legal. Other values are rounded down to one of the
permitted sizes, except values lower than 9 which are
rounded up.
DOSERR
stores the code of the last error. As error codes are
always positive numbers (see appendix A), zero can be used
as a no-error condition.
MacDOS updates this variable whenever an error (not a
warning) occurs, but does not reset it when a command
completes successfully. Therefore, it is your
responsibility to remove it or initialise it to zero.
You can obtain the error message corresponding to a
particular error code with the command SHOW.
SHOWAE
tells MacDOS whether it should display incoming Apple
Events.
MacDOS only checks whether this variable exists or not,
completely ignoring its value. For a list of Apple Events
supported by MacDOS, please refer to the section "MacDOS
Scripting".
TIMEOUT
Stores the number of seconds that MacDOS waits for piped
messages to go through a chain of MacDOS extensions. Please
refer to the section "MacDOS Extensions" for the details.